<template>
  <div>
    <!-- 面包屑导航 -->
    <el-bread></el-bread>

    <!-- 添加按钮 -->
    <el-button
      type="primary"
      @click="addDialog"
      class="addBtn"
      size="small"
      plain
      >添加</el-button
    >

    <!-- 表格主体 -->
    <el-table :data="specsList" border style="width: 100%">
      <el-table-column prop="id" label="规格编号" width="100"></el-table-column>

      <el-table-column
        prop="specsname"
        label="规格名称"
        width="130"
      ></el-table-column>

      <el-table-column prop="status" label="规格属性">
        <template v-slot="{ row }">
          <el-button
            v-for="item in row.attrs"
            :key="item"
            type="info"
            disabled
            size="mini"
            round
            >{{ item }}</el-button
          >
        </template>
      </el-table-column>

      <el-table-column prop="status" label="状态" width="100">
        <template v-slot="$prop">
          <el-button
            v-if="$prop.row.status == 1"
            type="success"
            disabled
            size="small"
            round
            >正常</el-button
          >
          <el-button v-else type="warning" size="small" disabled round
            >禁用</el-button
          >
        </template>
      </el-table-column>

      <el-table-column label="操作" width="120">
        <template v-slot="$prop">
          <el-button
            type="primary"
            @click="edit($prop.row.id)"
            icon="el-icon-edit"
            circle
          ></el-button>

          <el-button
            type="danger"
            @click="del($prop.row.id)"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @current-change="changePage"
      background
      :page-size="pageSize"
      layout="prev, pager, next"
      :total="total"
    >
    </el-pagination>

    <!-- Dialog弹窗 -->
    <v-dialog ref="diaLog"></v-dialog>
  </div>
</template>


<script>
//引入辅助性函数
import { mapState, mapActions } from "vuex";
//引入Dialog
import vDialog from "./dialog.vue";
//引入删除角色管理的接口
import { delSpecs } from "../../request/api";
export default {
  //计算属性 用来获取vuex中的数据 辅助性函数mapState
  computed: {
    ...mapState("specs", ["specsList", "pageSize", "total"]),
  },

  //方法 辅助性函数mapActions 和其他方法
  methods: {
    ...mapActions("specs", ["getSpecs", "getTotal", "getPage"]),

    //点击确认按钮
    addDialog() {
      //打开弹窗
      this.$store.commit("specs/changeDialog", true);
    },

    //修改页码数
    changePage(e) {
      this.getPage(e);
    },

    //删除事件
    del(id) {
      this.$del(() => {
        delSpecs({ id }).then((res) => {
          if (res.code == 200) {
            this.$message.success(res.msg);
            this.getSpecs();
          } else {
            this.$message.error(res.msg);
          }
        });
      });
    },

    //点击编辑事件
    edit(e){
      //打开弹窗
      this.$store.commit('specs/changeDialog',true);
      //传递 id 
      this.$refs.diaLog.dataForm(e);
    }
  },

  //挂载完成
  mounted() {
    //获取数据
    this.getSpecs();
    //获取总条数
    this.getTotal();
  },

  //注册组件
  components: {
    vDialog,
  },
};
</script>

<style lang='less' scoped>
//添加按钮的样式
.addBtn {
  margin: 15px 5px;
}
</style>